package com.myexample.hmsdemo.model.impl;

import android.content.Context;
import android.content.Intent;

import com.huawei.hmf.tasks.OnCompleteListener;
import com.huawei.hmf.tasks.Task;
import com.huawei.hms.support.account.AccountAuthManager;
import com.huawei.hms.support.account.request.AccountAuthParams;
import com.huawei.hms.support.account.request.AccountAuthParamsHelper;
import com.huawei.hms.support.account.result.AuthAccount;
import com.huawei.hms.support.account.service.AccountAuthService;
import com.myexample.hmsdemo.model.ILogin;

public class HuaweiLogin implements ILogin {
    private AccountAuthParams authParams;
    private AccountAuthService service;
    @Override
    public Intent getIntent(Context context) {
        authParams=new AccountAuthParamsHelper(AccountAuthParams.DEFAULT_AUTH_REQUEST_PARAM).setAuthorizationCode().createParams();
        service= AccountAuthManager.getService(context,authParams);
        return service.getSignInIntent();
    }

    @Override
    public AuthAccount getAccount(Intent data) {
        Task<AuthAccount>authAccountTask=AccountAuthManager.parseAuthResultFromIntent(data);
        if(authAccountTask.isSuccessful())
            return authAccountTask.getResult();
        return null;
    }

    @Override
    public void logout() {
        Task<Void>singOutTask=service.signOut();
        singOutTask.addOnCompleteListener(new OnCompleteListener<Void>() {
            @Override
            public void onComplete(Task<Void> task) {
                //登出操作
            }
        });
    }
}
